﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using frag_boardgame;
using GameService;
using System.Collections.Generic;

namespace Website
{
    public partial class gamelist : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Master.IsLoggedIn)
            {
                Response.Redirect("Default.aspx");
            }
            lblUserName.Text = Master.GetUserInfo().config.Name;
            lblScore.Text = Master.GetPlayerScore().ToString();

            RefreshGames();
            Timer1.Enabled = true;

        }

        protected void ButtonCreate_Click(object sender, EventArgs e)
        {
            RoomInfo roomInfo = null;
            try
            {
                GameConfiguration conf = new GameConfiguration();
                int tmp;
                if (int.TryParse(TextRows.Text, out tmp))
                    conf.Rows = tmp;
                else
                    conf.Rows = 16;

                if (int.TryParse(TextColumns.Text, out tmp))
                    conf.Cols = tmp;
                else
                    conf.Cols = 25;

                conf.NumberOfPlayers = int.Parse(TextNPlayers.Text);
                roomInfo = Master.GameManager.CreateRoom(TextName.Text, conf);

                if (roomInfo != null)
                {
                    LabelMessage.Text = "Room '" + roomInfo.name + "' created succesfully.";
                }
                else
                {
                    LabelMessage.Text = "Failed to create room '" + TextName.Text + "'.";
                }
            }
            catch (ArgumentException ex)
            {
                LabelMessage.Text = ex.Message;
            }
            LabelMessage.Visible = true;
            RefreshGames();

        }

        protected IEnumerable<Room> Rooms
        {
            get { return Master.GameManager.rooms.Values; }
        }

        protected void RefreshGames()
        {
            var gameSet = from room in Master.GameManager.rooms.Values
                          select new
                          {
                              Name = room.Info.name,
                              NCurrentPlayers = room.Info.nPlayers,
                              NTotalPlayers = room.Info.config.NumberOfPlayers,
                              Rows = room.Info.config.Rows,
                              Cols = room.Info.config.Cols,
                              isPlaying = room.IsPlaying
                          };
            GamesRepeater.DataSource = gameSet;
            GamesRepeater.DataBind();
        }

        protected void GamesRepeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Join")
            {

                LabelMessage.Text = "Joining room '" + e.CommandArgument + "'";
                LabelMessage.Visible = true;
                JoinRoomResult res = Master.JoinRoom((string)e.CommandArgument);
              
                if (res== JoinRoomResult.GameStarted)
                    LabelMessage.Text = "Game already started on '" + e.CommandArgument + "'";
                else if (res== JoinRoomResult.RoomIsFull)
                    LabelMessage.Text ="'"+ e.CommandArgument + "'"+" is full";
                else if (res == JoinRoomResult.RoomNotFound || res == JoinRoomResult.UserNotFound)
                    LabelMessage.Text = "Unknown error";
                else
                {
                    Timer1.Enabled = false;
                    Response.Redirect("configuration.aspx");
                }
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            RefreshGames();
            UpnlGames.Update();
        }

    }
}
